<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

/**
 * 系统角色-用户关系表
 */
class CreateSysRoleUserTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        //角色和用户多对多的关系表
        Schema::create('sys_role_users', function (Blueprint $table) {
            $table->unsignedInteger('role_id')->comment('权限ID');
            $table->unsignedInteger('user_id')->comment('角色ID');
            //外键
            $table->foreign('role_id')
                ->references('role_id')
                ->on('sys_roles')
                ->onDelete('cascade');

            $table->foreign('user_id')
                ->references('id')
                ->on('sys_users')
                ->onDelete('cascade');

            $table->primary(['role_id','user_id']);
            $table->softDeletes();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('sys_role_users');
    }
}
